<?php
include_once 'mod.php';
$mod = new mod();
$mysql = $mod->link();
$sql = 'select * from category';
$result = $mod->select($mysql,$sql);
if ($_SERVER['REQUEST_METHOD']=='POST'){
    if(empty($_POST['cid'])){
        echo '没有选择分类<a href="add.php">重新添加</a>';die;
    }
    if(empty($_POST['name'])){
        echo '没有输入名称<a href="add.php">重新添加</a>';die;
    }
    if(empty($_POST['price'])){
        echo '没有选输入价格<a href="add.php">重新添加</a>';die;
    }
    if(empty($_POST['stock'])){
        echo '没有输入库存<a href="add.php">重新添加</a>';die;
    }

    if(!isset($_POST['status'])){
        echo '没有选择状态<a href="add.php">重新添加</a>';die;
    }
    $fields = '`name`,cid,`status`,price,info,picture,create_time,update_time,stock,`desc`';

    $name = $_POST['name'];
    $cid = $_POST['cid'];
    $status = $_POST['status'];
    $price = $_POST['price'];
    $info = '';
    $picture = '';
    $create_time = time();
    $update_time = date('Y-m-d H:i:s');
    $stock = $_POST['stock'];
    $desc = $_POST['desc'];

    $values = "'{$name}',{$cid},{$status},{$price},'{$info}','{$picture}',{$create_time},'{$update_time}',{$stock},'{$desc}'";
    $sql = "insert into products({$fields}) values ({$values})";

    $result = $mod->IDU($mysql,$sql);
    if($result['flag']){
        echo "新增成功<a href='index.php'>回到首页</a>";die;
    }else{
        echo "新增失败({$result['msg']})<a href='index.php'>回到首页</a>";die;
    }
}

?>


<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>新增产品</title>
</head>
<body>
<form action="add.php" method="post">
    <p>分类：
        <select name="cid">
            <option value="">请选择分类</option>
            <?php foreach ($result as $item){?>
                <option value="<?= $item['id']?>"><?= $item['name']?></option>
            <?}?>

        </select>
    </p>
    <p>名称： <input type="text" name="name"></p>
    <p> 价格：<input type="text" name="price"></p>
    <p>库存:<input type="text" name="stock"></p>
    <p>状态:
        <input type="radio" value="1" name="status" >上架
        <input type="radio"  value="0" name="status" checked>下架
    </p>
    <p>描述:
        <textarea  name="desc"></textarea>
    </p>

    <button type="submit">新增</button>


</form>
</body>
</html>